package com.yisu.auction.manager.core.service.impl;

import com.yisu.auction.ewf.common.web.EasyUIGrid;
import com.yisu.auction.ewf.entity.Member;
import com.yisu.auction.ewf.entity.Prize;
import com.yisu.auction.manager.core.mapper.MemberMapper;
import com.yisu.auction.manager.core.mapper.PrizeArchiveMapper;
import com.yisu.auction.manager.core.mapper.PrizeMapper;
import com.yisu.auction.manager.core.service.IWinnerService;
import com.yisu.auction.manager.domain.request.memberPackage.WinnerRequest;
import com.yisu.auction.manager.domain.vo.winnerInfoPackageVos.WinnerVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * author: 黄萧林
 * Business Description：
 * createTime：2017/8/25
 */
@Service("IWinnerService")
public class WinnerServiceImpl extends BaseService implements IWinnerService {
    @Resource
    private PrizeMapper prizeMapper;
    @Resource
    private PrizeArchiveMapper prizeArchiveMapper;
    @Resource
    private MemberMapper memberMapper;


    @Override
    public EasyUIGrid<WinnerVo> getWinnerInfoByPage(WinnerRequest winnerRequest) {
        List<WinnerVo> winnerVos = prizeMapper.getWinnerInfoByPage(winnerRequest);
        EasyUIGrid<WinnerVo> grid = new EasyUIGrid<>();
        grid.setRows(winnerVos);
        grid.setTotal(winnerRequest.getTotalRecords());
        return grid;
    }

    @Override
    public Prize getReceivingGoodById(Long id) {
        return getPrizeById(id);
    }

    @Override
    public Boolean insertReceivingGoodAddress(Prize prize) {
        Prize prizeVo = getPrizeById(prize.getId());
        Prize checkPrize = prizeMapper.selectByPrimaryKey(prize.getId());
        Prize updatePrize = new Prize();
        updatePrize.setId(prize.getId());
        updatePrize.setExpressCompany(prize.getExpressCompany());
        updatePrize.setExpressNo(prize.getExpressNo());
        updatePrize.setSendTime(new Date());
        if (prizeVo.getIsRechargeableCard() == 1) {
            updatePrize.setConfirmTime(new Date());
            updatePrize.setOrderStatus(3);
        } else {
            updatePrize.setOrderStatus(2);
        }
        if (checkPrize == null) {
            if (prizeArchiveMapper.updateByPrimaryKeySelective(updatePrize) > 0) {
                if (prizeVo.getMemberId() != null) {
                    Member member = memberMapper.selectByPrimaryKey(prizeVo.getMemberId());
                    String pushContent = "恭喜您，奖品" + prizeVo.getName() + "已经发货,请查收！";
                    addSmsPush(pushContent, member.getMobile(), (byte) 2, member.getId(), prize.getId(), 0, new Date());
                    String systemNoticeTitle = "发货通知";
                    addSystemNotice(pushContent, member.getId(), systemNoticeTitle, systemNoticeTitle, 2);
                }
            }
        } else {
            return prizeMapper.updateByPrimaryKeySelective(updatePrize) > 0;
        }
        return true;
    }
}
